<<<<<<< HEAD
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Class acount extends CI_Controller
 * @author
 * 用户
 */
class User extends CRUD_Controller
{

    function __construct()
    {
        parent::__construct();

        $this->load->model('user_model','user','default');
        $this->rules = array(
            "create" => array(
                array(
                    'field'   => 'uname',
                    'label'   => '用户名',
                    'rules'   => 'trim|required'
                ),
                array(
                    'field'   => 'phone',
                    'label'   => '用户名',
                    'rules'   => 'trim|required|is_mobile|callback_phone_exist'
                ),
                array(
                    'field'   => 'pwd',
                    'label'   => '密码',
                    'rules'   => 'trim|required|min_length[6]|max_length[20]|regex_match[/^[\da-zA-Z]{6,20}$/]|matches[pwdre]'
                ),
                array(
                    'field'   => 'pwdre',
                    'label'   => '验证密码',
                    'rules'   => 'trim|required|min_length[6]|max_length[20]|regex_match[/^[\da-zA-Z]{6,20}$/]'
                ),
            ),
            "edit" => array(
                array(
                    'field'   => 'id',
                    'label'   => '帐号',
                    'rules'   => 'trim|required|numeric|callback_mid_check'
                ),
            ),
        );

    }
    public function _index_orders(){
        return array('timeline'=>'desc');
    }
    public function _index_where(){
        return array('is_delect'=>0,'type'=>1);
    }
    public function index($page=1){
        // TODO: 排序 config
        $limit = $this->page_limit;
        $this->input->get('limit') and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');

        // TODO 处理条件 order for search
        $where = $this->_index_where();
        if ($this->input->get('where')) {
        }
        $orders = $this->_index_orders();
        if ($this->input->get('order')) {
        }

        $fields = $this->_list_fields();
        $fields="uname,phone,email,id,timeline";
        //echo '<pre/>';
        $vdata['pages'] = $this->_pages(site_url($this->class.'/index'),$limit,$where);
        $list=$this->model->get_list($limit,$limit*($page-1),$orders,$where,$fields);
        $userids=array();
        if(!empty($list)){
            foreach($list as $k=>$v){
                $userids[]=$v['id'];//用户id集合
            }

            if(!empty($userids)){
                $company=$this->db->where_in('user_id',$userids)->select('id,user_id')->get('user_company')->result_array();
                $user_company=array();
                if(!empty($company)){
                    foreach($company as $v){
                        $user_company[$v['user_id']]=$v['id'];
                    }
                }
                foreach($list as $k=>$v){
                    if(isset($user_company[$v['id']])){
                        $list[$k]['is_company']=1;
                    }else{
                        $list[$k]['is_company']=0;
                    }
                }
            }
        }
        $vdata['list'] = $list;
        $this->_display($vdata);
    }
    //企业信息
    public function user_company($id){
        $this->load->model('common_model','common','default');
        $this->load->helpers('uisite_helper');
        $count=$this->user->countLog(array('id'=>$id,'type'=>1,'is_delect'=>0));
        if($count==0){
            goto_message('该用户不存在','user/index');
        }
        $company_count=$this->common->countLog(array('user_id'=>$id),'user_company');
        if($company_count==0){
            goto_message('该企业信息不存在','user/index');
        }
        $res=$this->common->onceInfo(array('user_id'=>$id),'*','user_company');
        if(!empty($res)){
            if(!empty($res['photo'])){
                $res['photo']=upload_file(tag_photo($res['photo']));
            }
            if(!empty($res['photo1'])){
                $res['photo1']=upload_file(tag_photo($res['photo1']));
            }
        }
        $vdata['it']=$res;
        $this->_display($vdata,'user_company.php');
    }
    //修改企业信息状态
    public function updateCompanyStatus(){
        $this->load->model('common_model','common','default');
        $data=$this->input->post(NULL,TRUE);
        $id=trim($data['id']);
        $company_count=$this->common->countLog(array('id'=>$id),'user_company');
        if($company_count==0){
            $back['msg'] = '该企业信息不存在！';
            $back['status'] = 1;
            exit(json_encode($back));
        }
        $res=$this->common->updateInfo('user_company',array('status'=>2),array('id'=>$id));
        if($res){
            $back['msg'] = '操作成功';
            $back['status'] = 1;
            exit(json_encode($back));
        }else{
            $back['msg'] = '操作失败';
            $back['status'] = 1;
            exit(json_encode($back));
        }
    }
    //修改企业信息状态
    public function updateCompanyStatusNo(){
        $this->load->model('common_model','common','default');
        $data=$this->input->post(NULL,TRUE);
        $id=trim($data['id']);
        $company_count=$this->common->countLog(array('id'=>$id),'user_company');
        if($company_count==0){
            $back['msg'] = '该企业信息不存在！';
            $back['status'] = 1;
            exit(json_encode($back));
        }
        $res=$this->common->updateInfo('user_company',array('status'=>3),array('id'=>$id));
        if($res){
            $back['msg'] = '操作成功';
            $back['status'] = 1;
            exit(json_encode($back));
        }else{
            $back['msg'] = '操作失败';
            $back['status'] = 1;
            exit(json_encode($back));
        }
    }
    //子账户分类信息
    public function subsidiaryAction($id,$page=1){
        $count=$this->user->countLog(array('id'=>$id,'type'=>1,'is_delect'=>0));
        if($count==0){
            goto_message('该用户不存在','user/index');
        }
        $this->load->model('common_model','common','default');
        $limit = $this->page_limit;
        $this->input->get('limit') and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');
        $list=$this->common->allInfoorder(array('user_id'=>$id),'id,title,timeline','id desc','user_action');
        $p=($page-1)*$limit;
        $count=count($list);
        $list=array_splice($list,$p,$limit);
        $vdata['list']=$list;
        $url = ADMINER_URL . 'index.php/user/subsidiaryAction/'.$id;
        $config=page_config($limit,$count,$url,4);
        $this->load->library('pagination');
        $this->pagination->initialize($config);
        $pages=$this->pagination->create_links();
        $vdata['list']=$list;
        $vdata['pages']=$pages;
        $this->_display($vdata,'user_action.php');
    }
    //添加vr链接
    public function updateCompanyVr(){
        $this->load->model('common_model','common','default');
        $data=$this->input->post(NULL,TRUE);
        $id=(int)trim($data['id']);
        $vr_url=trim($data['vr_url']);
        if(empty($id)){
            $back['msg'] = 'id不能为空';
            $back['status'] = 1;
            exit(json_encode($back));
        }
        if(empty($vr_url)){
            $back['msg'] = 'vr链接为空';
            $back['status'] = 1;
            exit(json_encode($back));
        }
        $count=$this->common->countLog(array('id'=>$id),'user_company');
        if($count==0){
            $back['msg'] = '该企业信息不存在';
            $back['status'] = 1;
            exit(json_encode($back));
        }
        $user=$this->common->onceInfo(array('id'=>$id),'user_id','user_company');
        $res=$this->common->updateInfo('user_company',array('vr_url'=>$vr_url),array('id'=>$id));
        if($res){
            $this->common->updateInfo('user',array('vr_url'=>$vr_url),array('id'=>$user['user_id']));
            $back['msg'] = '操作成功';
            $back['status'] = 1;
            exit(json_encode($back));
        }else{
            $back['msg'] = '操作失败';
            $back['status'] = 1;
            exit(json_encode($back));
        }
    }
    //修改VR工厂申请状态
    public function updateVrstatus(){
        $this->load->model('common_model','common','default');
        $data=$this->input->post(NULL,TRUE);
        $id=(int)trim($data['id']);
        $vr_status=(int)trim($data['vr_status']);
        if(empty($id)){
            $back['msg'] = 'id不能为空';
            $back['status'] = 1;
            exit(json_encode($back));
        }
        $count=$this->common->countLog(array('id'=>$id),'user_company');
        if($count==0){
            $back['msg'] = '该企业信息不存在';
            $back['status'] = 1;
            exit(json_encode($back));
        }
        $res=$this->common->onceInfo(array('id'=>$id),'vr_status','user_company');
        if($res['vr_status']==1){
            $v=$this->common->updateInfo('user_company',array('vr_status'=>$vr_status),array('id'=>$id));
            if($v){
                $back['msg'] = '操作成功';
                $back['status'] = 1;
                exit(json_encode($back));
            }else{
                $back['msg'] = '操作失败';
                $back['status'] = 1;
                exit(json_encode($back));
            }
        }else{
            $back['msg'] = '目前状态不能修改';
            $back['status'] = 1;
            exit(json_encode($back));
        }
    }



    //子账户信息
    public function subsidiary($id,$page=1){
        $this->load->model('common_model','common','default');
        $limit = $this->page_limit;
        $this->input->get('limit') and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');
        $list=$this->common->allInfoorder(array('fid'=>$id,'is_delect'=>0,'type'=>0),'id,uname,phone,action_name,timeline,email','id desc','user');
        $p=($page-1)*$limit;
        $count=count($list);
        $list=array_splice($list,$p,$limit);
        $vdata['list']=$list;
        $url = ADMINER_URL . 'index.php/user/subsidiaryAction/'.$id;
        $config=page_config($limit,$count,$url,4);
        $this->load->library('pagination');
        $this->pagination->initialize($config);
        $pages=$this->pagination->create_links();
        $vdata['list']=$list;
        $vdata['pages']=$pages;
        $this->_display($vdata,'subsidiary.php');
    }


    protected function _create_data(){
        $form=$this->input->post();
        if(!empty($form['pwd'])){
            $form['password']= passwd($form['pwd']);
        }
        unset($form['pwd']);
        unset($form['pwdre']);
        $form['phone_valid']=1;
        $form['type']=1;
        $form['timeline']=now();
        return $form;
    }

//    public function acount_order($user_id="",$page=1){
//        $this->load->model('order_model','order','default');
//        $limit = $this->page_limit;
//        $this->input->get('limit') and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');
//        if(empty($user_id)){
//            $list=array();
//        }else{
//            $user=$this->order->onceInfo(array('id'=>$user_id),'uname','acount');
//            $list=$this->order->allInfoorder(array('user_id'=>$user_id,'is_delete'=>0),'id,order_num,name,mobile,tel,address,price,status,timeline','id desc','order');
//            if(!empty($list)){
//                $orderNum=array();
//                foreach($list as $k=>$v){
//                    $list[$k]['uname']=$user['uname'];
//                    $orderNum[]=$v['order_num'];
//                }
//                $orderdetail=array();
//                if(!empty($orderNum)){
//                    $order=$this->db->where_in('order_num',$orderNum)->select('order_num,name,product_id,id,sale_price,origin_price,combine_price,num,texture,color,size,type,status,is_share,share_id,thumb,is_group')->order_by('id desc')->get('order_detail')->result_array();
//                    foreach($order as $k=>$v){
//                        if($v['is_group']==1){
//                            $order[$k]['price']=$v['combine_price'];
//                        }else{
//                            if(!empty($v['sale_price'])){
//                                $order[$k]['price']=$v['sale_price'];
//                            }else{
//                                $order[$k]['price']=$v['origin_price'];
//                            }
//                        }
//                        if(!empty($v['thumb'])){
//                            $order[$k]['thumb']=upload_file($v['thumb']);
//                        }
//                        unset($order[$k]['sale_price']);
//                        unset($order[$k]['combine_price']);
//                    }
//                    foreach($order as $k=>$v){
//                        $orderdetail[$v['order_num']][]=$v;
//                    }
//                    foreach($orderdetail as $k=>$v){
//                        foreach($v as $i=>$it){
//                            unset($orderdetail[$k][$i]['order_num']);
//                        }
//                    }
//                }
//                //var_dump($orderdetail);
//                foreach($list as $k=>$v){
//                    if(isset($orderdetail[$v['order_num']])){
//                        $list[$k]['detail']=$orderdetail[$v['order_num']];
//                    }else{
//                        $list[$k]['detail']=array();
//                    }
//                }
//            }
//        }
//        $p=($page-1)*$limit;
//        $count=count($list);
//        $list=array_splice($list,$p,$limit);
//        $vdata['list']=$list;
//        $url = ADMINER_URL . 'index.php/acount/acount_order/'.$user_id;
//        $config=page_config($limit,$count,$url,4);
//        $this->load->library('pagination');
//        $this->pagination->initialize($config);
//        $pages=$this->pagination->create_links();
//        $vdata['list']=$list;
//        $vdata['pages']=$pages;
//        $this->_display($vdata,'acount_order.php');
//    }
//    // 搜索
    public function search($page=1){
        $title = "会员中心";
        //echo "十年前:".date("Y-m-d",strtotime("-10 year"))."<br>";  exit();
        $vdata['title'] = $title;
        $limit = $this->page_limit;
        $this->input->get('limit',TRUE) and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');
        $uname=$this->input->get('uname',TRUE);
        $sex=$this->input->get('sex',TRUE);
        $starttime=$this->input->get('starttime',TRUE);
        $endtime=$this->input->get('endtime',TRUE);
        $order_mark=$this->input->get('order',TRUE);
        $where=array('is_delect'=>0,'type'=>1);
        if(!empty($uname)){
            $where_q = array(
                'like phone'=> array('phone',$uname),
                'or_like uname'=> array('uname',$uname)
            );
            $where=array_merge($where,$where_q);
        }
        if(!empty($sex)){
            $where=array_merge($where,array('sex'=>$sex));
        }
        if(!empty($starttime)){
            $starttime=strtotime($starttime);
            $where=array_merge($where,array('timeline >= '=>$starttime));
        }
        if(!empty($endtime)){
            $endtime=strtotime($endtime);
            $where=array_merge($where,array('timeline <= '=>$endtime));
        }
        // var_dump($where);exit();
        $list=$this->db->where($where)->select('uname,phone,email,id,timeline')->get('user')->result_array();
        // var_dump($this->db->last_query());exit();
        if(!empty($list)){
            $userids=array();
            foreach($list as $k=>$v){
                $userids[]=$v['id'];//用户id集合
            }
            if(!empty($userids)){
                $company=$this->db->where_in('user_id',$userids)->select('id,user_id')->get('user_company')->result_array();
                $user_company=array();
                if(!empty($company)){
                    foreach($company as $v){
                        $user_company[$v['user_id']]=$v['id'];
                    }
                }
                foreach($list as $k=>$v){
                    if(isset($user_company[$v['id']])){
                        $list[$k]['is_company']=1;
                    }else{
                        $list[$k]['is_company']=0;
                    }
                }
            }
            $timeline_order=array();
            foreach($list as $k=>$v){
                $timeline_order[]=$v['timeline'];
            }
            if($order_mark==1){
                array_multisort($timeline_order,SORT_DESC,$list);//php中自带的排序方法
            }else if($order_mark==2){
                array_multisort($timeline_order,SORT_ASC,$list);//php中自带的排序方法
            }else{
                array_multisort($timeline_order,SORT_DESC,$list);//php中自带的排序方法
            }
        }
        $p=($page-1)*$limit;
        $count=count($list);
        $list=array_splice($list,$p,$limit);
        $vdata['list']=$list;
        $url = ADMINER_URL . 'index.php/user/search/';
        $config=page_config($limit,$count,$url,4);
        $this->load->library('pagination');
        $this->pagination->initialize($config);
        $pages=$this->pagination->create_links();
        $vdata['list']=$list;
        $vdata['pages']=$pages;
        $this->_display($vdata,$this->class.'_index.php');
    }




//    public function excel()
//    {
//
//        $where = array();
//        if ($ctype = $this->input->get('ctype',TRUE) AND is_numeric($ctype)) {
//            $where['ctype'] = $ctype;
//        }
//
//
//        if($this->input->get('q',TRUE)){
//            $where_q = array(
//                'like phone'=> array('phone',$this->input->get('q',TRUE)),
//                'or_like nick_name'=> array('nick_name',$this->input->get('q',TRUE))
//            );
//            $where = array_merge($where,$where_q);
//        }
//
//        $datalist=$this->model->get_all($where);
//
//        $filename = md5(time()).".xls";
//        header ( "Content-type:application/vnd.ms-excel;charset=utf-8" );
//        header ( "Content-Disposition:filename=".$filename );
//        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
//        header('Expires:0');
//        header('Pragma:public');
//        echo " <table border='1'>
//                    <tr>
//
//                      <td>手机</td>
//                      <td>昵称</td>
//                      <td>邮箱</td>
//                      <td>注册时间</td>
//
//
//                    </tr>";
//        foreach($datalist as $v){
//            echo " <tr>
//
//                  <td style=\"vnd.ms-excel.numberformat:@\" align='center'>".$v['phone']."</td>
//                  <td align='center'>".$v['nick_name']."</td>
//                  <td align='center'>".$v['email']."</td>
//                  <td align='center'>".date("Y-m-d",$v['timeline'])."</td>
//                </tr>";
//        }
//        echo "</table>" ;
//    }

    public function uname_exist($uname=false){
        $this->load->model('user_model','user','default');
        $count=$this->user->countLog(array('uname'=>$uname),'acount');
        if($count>0){
            $this->form_validation->set_message('uname_exist', '用户名已存在');
            return FALSE;
        }
        return TRUE;
    }
    public function phone_exist($phone=false){
        $this->load->model('user_model','user','default');
        $count=$this->user->countLog(array('phone'=>$phone,'phone_valid'=>1),'user');
        if($count>0){
            $this->form_validation->set_message('phone_exist', '该手机号已注册');
            return FALSE;
        }
        return TRUE;
    }
    private function checkPhone($phone)
    {
        if (preg_match('/^1[2345678]\d{9}$/', $phone)) {
            return true;
        } else {
            return false;
        }
    }
}
=======
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Class acount extends CI_Controller
 * @author
 * 用户
 */
class User extends CRUD_Controller
{

    function __construct()
    {
        parent::__construct();

        $this->load->model('user_model','user','default');
        $this->rules = array(
            "create" => array(
                array(
                    'field'   => 'uname',
                    'label'   => '用户名',
                    'rules'   => 'trim|required'
                ),
                array(
                    'field'   => 'phone',
                    'label'   => '用户名',
                    'rules'   => 'trim|required|is_mobile|callback_phone_exist'
                ),
                array(
                    'field'   => 'pwd',
                    'label'   => '密码',
                    'rules'   => 'trim|required|min_length[6]|max_length[20]|regex_match[/^[\da-zA-Z]{6,20}$/]|matches[pwdre]'
                ),
                array(
                    'field'   => 'pwdre',
                    'label'   => '验证密码',
                    'rules'   => 'trim|required|min_length[6]|max_length[20]|regex_match[/^[\da-zA-Z]{6,20}$/]'
                ),
            ),
            "edit" => array(
                array(
                    'field'   => 'id',
                    'label'   => '帐号',
                    'rules'   => 'trim|required|numeric|callback_mid_check'
                ),
            ),
        );

    }
    public function _index_orders(){
        return array('timeline'=>'desc');
    }
    public function _index_where(){
        return array('is_delect'=>0,'type'=>1);
    }
    public function index($page=1){
        // TODO: 排序 config
        $limit = $this->page_limit;
        $this->input->get('limit') and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');

        // TODO 处理条件 order for search
        $where = $this->_index_where();
        if ($this->input->get('where')) {
        }
        $orders = $this->_index_orders();
        if ($this->input->get('order')) {
        }

        $fields = $this->_list_fields();
        $fields="uname,phone,email,id,timeline";
        //echo '<pre/>';
        $vdata['pages'] = $this->_pages(site_url($this->class.'/index'),$limit,$where);
        $list=$this->model->get_list($limit,$limit*($page-1),$orders,$where,$fields);
        $userids=array();
        if(!empty($list)){
            foreach($list as $k=>$v){
                $userids[]=$v['id'];//用户id集合
            }

            if(!empty($userids)){
                $company=$this->db->where_in('user_id',$userids)->select('id,user_id')->get('user_company')->result_array();
                $user_company=array();
                if(!empty($company)){
                    foreach($company as $v){
                        $user_company[$v['user_id']]=$v['id'];
                    }
                }
                foreach($list as $k=>$v){
                    if(isset($user_company[$v['id']])){
                        $list[$k]['is_company']=1;
                    }else{
                        $list[$k]['is_company']=0;
                    }
                }
            }
        }
        $vdata['list'] = $list;
        $this->_display($vdata);
    }
    //企业信息
    public function user_company($id){
        $this->load->model('common_model','common','default');
        $this->load->helpers('uisite_helper');
        $count=$this->user->countLog(array('id'=>$id,'type'=>1,'is_delect'=>0));
        if($count==0){
            goto_message('该用户不存在','user/index');
        }
        $company_count=$this->common->countLog(array('user_id'=>$id),'user_company');
        if($company_count==0){
            goto_message('该企业信息不存在','user/index');
        }
        $res=$this->common->onceInfo(array('user_id'=>$id),'*','user_company');
        if(!empty($res)){
            if(!empty($res['photo'])){
                $res['photo']=upload_file(tag_photo($res['photo']));
            }
            if(!empty($res['photo1'])){
                $res['photo1']=upload_file(tag_photo($res['photo1']));
            }
        }
        $vdata['it']=$res;
        $this->_display($vdata,'user_company.php');
    }
    //修改企业信息状态
    public function updateCompanyStatus(){
        $this->load->model('common_model','common','default');
        $data=$this->input->post(NULL,TRUE);
        $id=trim($data['id']);
        $company_count=$this->common->countLog(array('id'=>$id),'user_company');
        if($company_count==0){
            $back['msg'] = '该企业信息不存在！';
            $back['status'] = 1;
            exit(json_encode($back));
        }
        $res=$this->common->updateInfo('user_company',array('status'=>2),array('id'=>$id));
        if($res){
            $back['msg'] = '操作成功';
            $back['status'] = 1;
            exit(json_encode($back));
        }else{
            $back['msg'] = '操作失败';
            $back['status'] = 1;
            exit(json_encode($back));
        }
    }
    //修改企业信息状态
    public function updateCompanyStatusNo(){
        $this->load->model('common_model','common','default');
        $data=$this->input->post(NULL,TRUE);
        $id=trim($data['id']);
        $company_count=$this->common->countLog(array('id'=>$id),'user_company');
        if($company_count==0){
            $back['msg'] = '该企业信息不存在！';
            $back['status'] = 1;
            exit(json_encode($back));
        }
        $res=$this->common->updateInfo('user_company',array('status'=>3),array('id'=>$id));
        if($res){
            $back['msg'] = '操作成功';
            $back['status'] = 1;
            exit(json_encode($back));
        }else{
            $back['msg'] = '操作失败';
            $back['status'] = 1;
            exit(json_encode($back));
        }
    }
    //子账户分类信息
    public function subsidiaryAction($id,$page=1){
        $count=$this->user->countLog(array('id'=>$id,'type'=>1,'is_delect'=>0));
        if($count==0){
            goto_message('该用户不存在','user/index');
        }
        $this->load->model('common_model','common','default');
        $limit = $this->page_limit;
        $this->input->get('limit') and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');
        $list=$this->common->allInfoorder(array('user_id'=>$id),'id,title,timeline','id desc','user_action');
        $p=($page-1)*$limit;
        $count=count($list);
        $list=array_splice($list,$p,$limit);
        $vdata['list']=$list;
        $url = ADMINER_URL . 'index.php/user/subsidiaryAction/'.$id;
        $config=page_config($limit,$count,$url,4);
        $this->load->library('pagination');
        $this->pagination->initialize($config);
        $pages=$this->pagination->create_links();
        $vdata['list']=$list;
        $vdata['pages']=$pages;
        $this->_display($vdata,'user_action.php');
    }
    //添加vr链接
    public function updateCompanyVr(){
        $this->load->model('common_model','common','default');
        $data=$this->input->post(NULL,TRUE);
        $id=(int)trim($data['id']);
        $vr_url=trim($data['vr_url']);
        if(empty($id)){
            $back['msg'] = 'id不能为空';
            $back['status'] = 1;
            exit(json_encode($back));
        }
        if(empty($vr_url)){
            $back['msg'] = 'vr链接为空';
            $back['status'] = 1;
            exit(json_encode($back));
        }
        $count=$this->common->countLog(array('id'=>$id),'user_company');
        if($count==0){
            $back['msg'] = '该企业信息不存在';
            $back['status'] = 1;
            exit(json_encode($back));
        }
        $user=$this->common->onceInfo(array('id'=>$id),'user_id','user_company');
        $res=$this->common->updateInfo('user_company',array('vr_url'=>$vr_url),array('id'=>$id));
        if($res){
            $this->common->updateInfo('user',array('vr_url'=>$vr_url),array('id'=>$user['user_id']));
            $back['msg'] = '操作成功';
            $back['status'] = 1;
            exit(json_encode($back));
        }else{
            $back['msg'] = '操作失败';
            $back['status'] = 1;
            exit(json_encode($back));
        }
    }
    //修改VR工厂申请状态
    public function updateVrstatus(){
        $this->load->model('common_model','common','default');
        $data=$this->input->post(NULL,TRUE);
        $id=(int)trim($data['id']);
        $vr_status=(int)trim($data['vr_status']);
        if(empty($id)){
            $back['msg'] = 'id不能为空';
            $back['status'] = 1;
            exit(json_encode($back));
        }
        $count=$this->common->countLog(array('id'=>$id),'user_company');
        if($count==0){
            $back['msg'] = '该企业信息不存在';
            $back['status'] = 1;
            exit(json_encode($back));
        }
        $res=$this->common->onceInfo(array('id'=>$id),'vr_status','user_company');
        if($res['vr_status']==1){
            $v=$this->common->updateInfo('user_company',array('vr_status'=>$vr_status),array('id'=>$id));
            if($v){
                $back['msg'] = '操作成功';
                $back['status'] = 1;
                exit(json_encode($back));
            }else{
                $back['msg'] = '操作失败';
                $back['status'] = 1;
                exit(json_encode($back));
            }
        }else{
            $back['msg'] = '目前状态不能修改';
            $back['status'] = 1;
            exit(json_encode($back));
        }
    }



    //子账户信息
    public function subsidiary($id,$page=1){
        $this->load->model('common_model','common','default');
        $limit = $this->page_limit;
        $this->input->get('limit') and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');
        $list=$this->common->allInfoorder(array('fid'=>$id,'is_delect'=>0,'type'=>0),'id,uname,phone,action_name,timeline,email','id desc','user');
        $p=($page-1)*$limit;
        $count=count($list);
        $list=array_splice($list,$p,$limit);
        $vdata['list']=$list;
        $url = ADMINER_URL . 'index.php/user/subsidiaryAction/'.$id;
        $config=page_config($limit,$count,$url,4);
        $this->load->library('pagination');
        $this->pagination->initialize($config);
        $pages=$this->pagination->create_links();
        $vdata['list']=$list;
        $vdata['pages']=$pages;
        $this->_display($vdata,'subsidiary.php');
    }


    protected function _create_data(){
        $form=$this->input->post();
        if(!empty($form['pwd'])){
            $form['password']= passwd($form['pwd']);
        }
        unset($form['pwd']);
        unset($form['pwdre']);
        $form['phone_valid']=1;
        $form['type']=1;
        $form['timeline']=now();
        return $form;
    }

//    public function acount_order($user_id="",$page=1){
//        $this->load->model('order_model','order','default');
//        $limit = $this->page_limit;
//        $this->input->get('limit') and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');
//        if(empty($user_id)){
//            $list=array();
//        }else{
//            $user=$this->order->onceInfo(array('id'=>$user_id),'uname','acount');
//            $list=$this->order->allInfoorder(array('user_id'=>$user_id,'is_delete'=>0),'id,order_num,name,mobile,tel,address,price,status,timeline','id desc','order');
//            if(!empty($list)){
//                $orderNum=array();
//                foreach($list as $k=>$v){
//                    $list[$k]['uname']=$user['uname'];
//                    $orderNum[]=$v['order_num'];
//                }
//                $orderdetail=array();
//                if(!empty($orderNum)){
//                    $order=$this->db->where_in('order_num',$orderNum)->select('order_num,name,product_id,id,sale_price,origin_price,combine_price,num,texture,color,size,type,status,is_share,share_id,thumb,is_group')->order_by('id desc')->get('order_detail')->result_array();
//                    foreach($order as $k=>$v){
//                        if($v['is_group']==1){
//                            $order[$k]['price']=$v['combine_price'];
//                        }else{
//                            if(!empty($v['sale_price'])){
//                                $order[$k]['price']=$v['sale_price'];
//                            }else{
//                                $order[$k]['price']=$v['origin_price'];
//                            }
//                        }
//                        if(!empty($v['thumb'])){
//                            $order[$k]['thumb']=upload_file($v['thumb']);
//                        }
//                        unset($order[$k]['sale_price']);
//                        unset($order[$k]['combine_price']);
//                    }
//                    foreach($order as $k=>$v){
//                        $orderdetail[$v['order_num']][]=$v;
//                    }
//                    foreach($orderdetail as $k=>$v){
//                        foreach($v as $i=>$it){
//                            unset($orderdetail[$k][$i]['order_num']);
//                        }
//                    }
//                }
//                //var_dump($orderdetail);
//                foreach($list as $k=>$v){
//                    if(isset($orderdetail[$v['order_num']])){
//                        $list[$k]['detail']=$orderdetail[$v['order_num']];
//                    }else{
//                        $list[$k]['detail']=array();
//                    }
//                }
//            }
//        }
//        $p=($page-1)*$limit;
//        $count=count($list);
//        $list=array_splice($list,$p,$limit);
//        $vdata['list']=$list;
//        $url = ADMINER_URL . 'index.php/acount/acount_order/'.$user_id;
//        $config=page_config($limit,$count,$url,4);
//        $this->load->library('pagination');
//        $this->pagination->initialize($config);
//        $pages=$this->pagination->create_links();
//        $vdata['list']=$list;
//        $vdata['pages']=$pages;
//        $this->_display($vdata,'acount_order.php');
//    }
//    // 搜索
    public function search($page=1){
        $title = "会员中心";
        //echo "十年前:".date("Y-m-d",strtotime("-10 year"))."<br>";  exit();
        $vdata['title'] = $title;
        $limit = $this->page_limit;
        $this->input->get('limit',TRUE) and is_numeric($this->input->get('limit')) AND $limit = $this->input->get('limit');
        $uname=$this->input->get('uname',TRUE);
        $sex=$this->input->get('sex',TRUE);
        $starttime=$this->input->get('starttime',TRUE);
        $endtime=$this->input->get('endtime',TRUE);
        $order_mark=$this->input->get('order',TRUE);
        $where=array('is_delect'=>0,'type'=>1);
        if(!empty($uname)){
            $where_q = array(
                'like phone'=> array('phone',$uname),
                'or_like uname'=> array('uname',$uname)
            );
            $where=array_merge($where,$where_q);
        }
        if(!empty($sex)){
            $where=array_merge($where,array('sex'=>$sex));
        }
        if(!empty($starttime)){
            $starttime=strtotime($starttime);
            $where=array_merge($where,array('timeline >= '=>$starttime));
        }
        if(!empty($endtime)){
            $endtime=strtotime($endtime);
            $where=array_merge($where,array('timeline <= '=>$endtime));
        }
        // var_dump($where);exit();
        $list=$this->db->where($where)->select('uname,phone,email,id,timeline')->get('user')->result_array();
        // var_dump($this->db->last_query());exit();
        if(!empty($list)){
            $userids=array();
            foreach($list as $k=>$v){
                $userids[]=$v['id'];//用户id集合
            }
            if(!empty($userids)){
                $company=$this->db->where_in('user_id',$userids)->select('id,user_id')->get('user_company')->result_array();
                $user_company=array();
                if(!empty($company)){
                    foreach($company as $v){
                        $user_company[$v['user_id']]=$v['id'];
                    }
                }
                foreach($list as $k=>$v){
                    if(isset($user_company[$v['id']])){
                        $list[$k]['is_company']=1;
                    }else{
                        $list[$k]['is_company']=0;
                    }
                }
            }
            $timeline_order=array();
            foreach($list as $k=>$v){
                $timeline_order[]=$v['timeline'];
            }
            if($order_mark==1){
                array_multisort($timeline_order,SORT_DESC,$list);//php中自带的排序方法
            }else if($order_mark==2){
                array_multisort($timeline_order,SORT_ASC,$list);//php中自带的排序方法
            }else{
                array_multisort($timeline_order,SORT_DESC,$list);//php中自带的排序方法
            }
        }
        $p=($page-1)*$limit;
        $count=count($list);
        $list=array_splice($list,$p,$limit);
        $vdata['list']=$list;
        $url = ADMINER_URL . 'index.php/user/search/';
        $config=page_config($limit,$count,$url,4);
        $this->load->library('pagination');
        $this->pagination->initialize($config);
        $pages=$this->pagination->create_links();
        $vdata['list']=$list;
        $vdata['pages']=$pages;
        $this->_display($vdata,$this->class.'_index.php');
    }




//    public function excel()
//    {
//
//        $where = array();
//        if ($ctype = $this->input->get('ctype',TRUE) AND is_numeric($ctype)) {
//            $where['ctype'] = $ctype;
//        }
//
//
//        if($this->input->get('q',TRUE)){
//            $where_q = array(
//                'like phone'=> array('phone',$this->input->get('q',TRUE)),
//                'or_like nick_name'=> array('nick_name',$this->input->get('q',TRUE))
//            );
//            $where = array_merge($where,$where_q);
//        }
//
//        $datalist=$this->model->get_all($where);
//
//        $filename = md5(time()).".xls";
//        header ( "Content-type:application/vnd.ms-excel;charset=utf-8" );
//        header ( "Content-Disposition:filename=".$filename );
//        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
//        header('Expires:0');
//        header('Pragma:public');
//        echo " <table border='1'>
//                    <tr>
//
//                      <td>手机</td>
//                      <td>昵称</td>
//                      <td>邮箱</td>
//                      <td>注册时间</td>
//
//
//                    </tr>";
//        foreach($datalist as $v){
//            echo " <tr>
//
//                  <td style=\"vnd.ms-excel.numberformat:@\" align='center'>".$v['phone']."</td>
//                  <td align='center'>".$v['nick_name']."</td>
//                  <td align='center'>".$v['email']."</td>
//                  <td align='center'>".date("Y-m-d",$v['timeline'])."</td>
//                </tr>";
//        }
//        echo "</table>" ;
//    }

    public function uname_exist($uname=false){
        $this->load->model('user_model','user','default');
        $count=$this->user->countLog(array('uname'=>$uname),'acount');
        if($count>0){
            $this->form_validation->set_message('uname_exist', '用户名已存在');
            return FALSE;
        }
        return TRUE;
    }
    public function phone_exist($phone=false){
        $this->load->model('user_model','user','default');
        $count=$this->user->countLog(array('phone'=>$phone,'phone_valid'=>1),'user');
        if($count>0){
            $this->form_validation->set_message('phone_exist', '该手机号已注册');
            return FALSE;
        }
        return TRUE;
    }
    private function checkPhone($phone)
    {
        if (preg_match('/^1[2345678]\d{9}$/', $phone)) {
            return true;
        } else {
            return false;
        }
    }
}
>>>>>>> 354f4b2fb2288b3c61dba6a59b3136f80f4b6909
